<?php
/**
 * Created by PhpStorm.
 * User: Tangyijun
 * Date: 2017/3/20
 * Time: 14:44
 */
namespace app\home\controller;
use app\models\Bases;
use think\Controller;
use think\Request;

class Base extends Controller{
    protected $TYPE_CODE = 1;    //1.核心内参yes
    protected $TYPE_STOCK = 2;   //2.每日牛股
    protected $TYPE_SPLENDID = 3;//3.精彩回放yes
    protected $TYPE_ADVERT = 4;  //4.多图广告
    /**
     * ajax请求返回成功信息
     * @param  string $data
     * @return array
     */
    public static function formatSuccessResult($data = null){
        return self::formatResult(0, '操作成功', $data);
    }
    /**
     * ajax请求返回错误信息
     * @param int $code
     * @param string $errorMsg
     * @param string $data
     * @return array
     */
    public static function formatResult($code, $errorMsg, $data = null){
        return array('code' => $code,'msg' => $errorMsg,'data'=>$data);
    }

    /**
     * @param $where
     * @return array|false|mixed|\PDOStatement|string|\think\Model
     * 根据条件查询用户信息
     */
    public static function getUserInfo($where){
        $user = new Bases('user');
        $userInfo = $user->find($where);
        if(empty($userInfo)){
            throw new \LogicException('该用户不存在',10011);
        }
        return $userInfo;
    }

    /**
     * @param $password
     * @param $repassword
     * @return bool
     * 主要作为登录验证密码是否正确
     */
    public static function checkPassword($password,$repassword){
        if(!password_verify($password,$repassword)){
            throw new \LogicException('输入的密码不正确',10012);
        }
        return true;
    }

    /**
     * @return bool
     * 验证是否是post提交
     */
    public static function checkSubmit(){
        if(!request()->isPost()){
            throw new \LogicException('请求方式错误',10013);
        }
        return true;
    }

    /**
     * @param $code
     * @param $action
     * @param $username
     * @return bool
     * 验证手机验证码
     */
    public static function checkCode($code,$action,$username){
        $reCode = cookie($username.$action);
        if($code!==$reCode){
            throw new \LogicException('手机验证码不正确',10014);
        }
        return true;
    }

    /**
     * @param $captcha
     * 验证网页验证码
     */
    public static function checkCaptcha($captcha){
        if(!captcha_check($captcha)){
            throw new \LogicException('网页验证码不正确',10015);
        };
    }

    /**
     * @return array|bool|false|mixed|\PDOStatement|string|\think\Model
     * 验证用户是否登录，登录则获取用户信息
     */
    public static function getUserInfoByCookie(){
        $uid = checkLogin();
        if($uid){
            $userInfo = self::getUserInfo("uid = $uid");
            return $userInfo;
        }
        return false;
    }


    public static function test(){
        try{
            $uid = 8;
            if($uid == 8){
                throw new \LogicException('用户ID不正确',10085);
            }
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
    }


}